package  com.syqc.sensor.recent

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.utils.MLog
import com.syqc.entity.ListInfo
import com.syqc.entity.OilCfg
import com.syqc.entity.OilDay
import com.syqc.entity.StatsType
import com.syqc.net.ServiceHelper
import com.syqc.base.BaseReportViewModel
import com.syqc.comlib.utils.GsonUtil
import com.syqc.monitor.R
import com.syqc.utils.DataUtil
import com.syqc.utils.ToolsUtil

class RecentOilVM : BaseReportViewModel(true, true) {

    //自定义开始时间
    var dStart = ""

    //自定义结束时间
    var dEnd = ""

    private val _dataLive = MutableLiveData<ArrayList<StatsType>>()
    var dataLive: LiveData<ArrayList<StatsType>> = _dataLive

    //模拟量
    private val _cfgLive = MutableLiveData<Int>()
    val cfgLiv: LiveData<Int> = _cfgLive

    override fun getData(carId: String, startTime: String, endTime: String) {
        super.getData(carId, startTime, endTime)
        toastLoading(true)
        getOilStats(carId, startTime, endTime)
    }

    //获取油量
    private fun getOilStats(carId: String, startTime: String, endTime: String) {
        ServiceHelper.getRepository()
            .getDayStats(1, carId, startTime, endTime)
            .autoDispose(this)
            .subscribe({
                MLog.e("油量数据===== $it")
                if (it.isNullOrEmpty()) {
                    _cfgLive.postValue(R.string.toast_no_oil_data)
                    toastLoading(false)
                    return@subscribe
                }
                if (it[0].cfg == null) {
                    _cfgLive.postValue(R.string.toast_no_set_analog)
                    toastLoading(false)
                    return@subscribe
                }
                handleData(it)
            }, error)
    }

    private val types = ArrayList<OilCfg>()


    private fun handleData(data: ArrayList<OilDay>) {
        types.clear()
        val list=ArrayList<OilDay>()
        data.forEach { dayInfo ->
            val oilCfg = GsonUtil.formObject<OilCfg>(dayInfo.cfg)
            if (oilCfg.name != null) {
                hasType(oilCfg)
                dayInfo.name = oilCfg.name
                dayInfo.precision = oilCfg.precision
                dayInfo.unit = oilCfg.unit
                dayInfo.upTotal = ToolsUtil.formitNumberD(oilCfg.precision, dayInfo.upTotal)
                dayInfo.leakTotal =
                    ToolsUtil.formitNumberD(dayInfo.precision, dayInfo.leakTotal)
                val runTotal = dayInfo.downTotal - dayInfo.idlingTotal
                dayInfo.runTotal =
                    ToolsUtil.formitNumberD(dayInfo.precision, if (runTotal < 0) 0f else runTotal)
                dayInfo.downTotal =
                    ToolsUtil.formitNumberD(dayInfo.precision, dayInfo.downTotal)
                dayInfo.idlingTotal =
                    ToolsUtil.formitNumberD(dayInfo.precision, dayInfo.idlingTotal)

                val mile = dayInfo.mile / 1000.0
                dayInfo.mileKm = ToolsUtil.formitNumberD(dayInfo.precision, mile)

                val total100 = if (mile == 0.0) 0.0 else dayInfo.downTotal / mile * 100.0
                val run100 = if (mile == 0.0) 0.0 else dayInfo.runTotal / mile * 100.0

                dayInfo.totalBy100km = ToolsUtil.formitNumberD(dayInfo.precision, total100)
                dayInfo.runBy100km = ToolsUtil.formitNumberD(dayInfo.precision, run100)
                list.add(dayInfo)
            }

        }

        val arrayList = ArrayList<StatsType>()
        types.sortBy { it.typeId }
        types.forEach { cfg ->
            val statsType = StatsType(cfg.typeId!!, cfg.name!!, cfg.unit, cfg.precision)
            list.forEach { day ->
                if (day.typeId == cfg.typeId)
                    statsType.info.add(day)
            }
            arrayList.add(statsType)
        }
        DataUtil.dayStats = arrayList
        toastLoading(false)
        _dataLive.postValue(arrayList)
    }

    /**
     *判断是否含有同类型
     *
     * @param oilCfg
     * @return
     */
    private fun hasType(oilCfg: OilCfg): Boolean {
        for (type in types) {
            if (type.typeId == oilCfg.typeId) return true
        }
        types.add(oilCfg)
        return false
    }

}